import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';

part 'package_group.g.dart';

@JsonSerializable(explicitToJson: true)
class PackageGroup {
  @JsonKey(defaultValue: [])
  List<CommodityFormList> commodityFormList;

  String commodityInfo;

  @JsonKey(defaultValue: 1)
  int count;

  String ctime;

  int deleted;

  @JsonKey(defaultValue: 0)
  int duplicated;

  bool get isDuplicated => duplicated == 1;

  String groupName;

  int id;

  int storeId;

  String utime;

  PackageGroup(
    this.commodityFormList,
    this.commodityInfo,
    this.count,
    this.ctime,
    this.deleted,
    this.duplicated,
    this.groupName,
    this.id,
    this.storeId,
    this.utime,
  );

  factory PackageGroup.fromJson(Map<String, dynamic> srcJson) =>
      _$PackageGroupFromJson(srcJson);

  Map<String, dynamic> toJson() => _$PackageGroupToJson(this);
}

@JsonSerializable()
class CommodityFormList {
  @JsonKey(ignore: true)
  GlobalKey key;

  int commodityId;

  String commodityName;

  int defaulted;

  bool get isDefaulted => defaulted == 1;

  int price;

  String priceText;

  int skuId;

  String skuName;

  String unit;

  @JsonKey(defaultValue: 0)
  int quantity;

  String get commodityNameWithSku => '$commodityName${skuName == null ? '' : '($skuName)'}';

  CommodityFormList(
    this.commodityId,
    this.commodityName,
    this.defaulted,
    this.price,
    this.skuId,
    this.skuName,
    this.unit,
  ) : key = GlobalKey();

  factory CommodityFormList.fromJson(Map<String, dynamic> srcJson) =>
      _$CommodityFormListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommodityFormListToJson(this);
}
